package server;

import java.sql.SQLException;
import java.util.ArrayList;

import javax.mail.MessagingException;
import javax.swing.JOptionPane;

public class ReturnsThread extends Thread {
	public MailSystem ms;
	public mysqlConnection sqlCon;

    /**
     * Starts the thread.
     * 
     * @param name name of the thread.
     */
	public ReturnsThread(String name, mysqlConnection c) {
            super(name);
            sqlCon=c;
    }

    /**
     * runs the thread that checks every 24 hours for returns
     */
    @SuppressWarnings("static-access")
	public void run() 
    {
            while (true) 
            {
                ArrayList<Object> messages = new ArrayList<Object>();
                
                messages = sqlCon.checkReturns();
                String[] users;
                String email;
                String copy;
                String username;
                
                for(int i=0;i<messages.size();i++)
                {
                	users = (String[]) messages.get(i);
                	email = users[1];
                	copy = users[0];
                	username = users[2];
                	try {
						ms.Send("ortbraudelibraryg3", "atuda2010", email, "Return Announcement", "Hello "+username+"\n You Have To Return Copy "+copy+" By Tomorrow");
					} catch (MessagingException e) {
						JOptionPane.showMessageDialog(null, "Couldn't send EMAIL.",
                                "ERROR", JOptionPane.ERROR_MESSAGE);
						e.printStackTrace();
					}
    
                }
                try {
					sqlCon.checkFreeze();
				} catch (SQLException e1) {
					// TODO Auto-generated catch block
					e1.printStackTrace();
				}
                try {
                        Thread.sleep(86400000);
                } catch (InterruptedException e) {
                        e.printStackTrace();
                }
            }

    }

}